﻿using System.Collections.Generic;
using Castle.ActiveRecord;
using Light.Utility;
using Light.Framework.Model;

namespace Light.Weixin.Model
{

    /// <summary>
    /// 消息模板
    /// </summary>
    [ActiveRecord]
    [ImportField("消息模板")]
    public class WxMsgTpl : Entity
    {
        /// <summary>
        /// 模板名称
        /// </summary>
        [Property("FName")]
        [ImportField("模板名称")]
        [Searchable("名称")]
        public string Name { get; set; }


        /// <summary>
        /// 模板代码
        /// </summary>
        [Property("FKey")]
        public string Key { get; set; }

        /// <summary>
        /// 公众平台的 模板ID
        /// </summary>
        [Property("FNumber")]
        [ImportField("模板编号")]
        [Searchable("编号")]
        public string Number { get; set; }

        /// <summary>
        /// 消息内容映射关系
        /// </summary>
        [Property]
        [ImportField("映射关系")]
        public string Mapping { get; set; }

        /// <summary>
        /// 第一句话的字体颜色
        /// </summary>
        [Property]
        public string ColorFirstWords
        {
            get
            {
                return this.GetValue<string>("ColorFirstWords", "blue");
            }
            set
            {
                this.SetValue("ColorFirstWords", value);
            }
        }

        /// <summary>
        /// 主体内容的字体颜色
        /// </summary>
        [Property]
        public string ColorBody
        {
            get
            {
                return this.GetValue<string>("ColorBody", "black");
            }
            set
            {
                this.SetValue("ColorBody", value);
            }
        }

        /// <summary>
        /// 备注内容的字体颜色
        /// </summary>
        [Property]
        public string ColorRemark
        {
            get
            {
                return this.GetValue<string>("ColorRemark", "blue");
            }
            set
            {
                this.SetValue("ColorRemark", value);
            }
        }

        /// <summary>
        /// 所属公众号
        /// </summary>
        [BelongsTo("WxApp_ID")]
        public WxApp WxApp { get; set; }

        private Dictionary<string, string> body = null;
        public Dictionary<string, string> Body
        {
            get
            {
                if (body == null)
                    body = StringHelper.StringParToDictionary(Mapping, '\n', '=');
                return body;
            }
        }

        public override bool Equals(object obj)
        {
            WxMsgTpl tpl = obj as WxMsgTpl;

            if (tpl != null)
                return tpl.WxApp == WxApp && tpl.Key == Key;
            else
                return false;
        }
    }
}
